<html>
<head><meta charset="utf-8"><title>New Pattern API? · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html">New Pattern API?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="209492625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209492625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209492625">(Sep 09 2020 at 10:09)</a>:</h4>
<p>I've been thinking about the Pattern API and how it could be improved (and maybe set off toward stabilization). I have a sketch here of an API which I think is substantially improved in a few important ways:</p>
<ul>
<li>It is not parameterized by a weird lifetime.</li>
<li>It does not have the searcher own the haystack, so algorithms can possibly mutate the haystack while searching it</li>
<li>It allows haystacks of types other than <code>str</code> (intended to be slices, primarily)</li>
</ul>
<p>Here's a gist:</p>
<p><a href="https://gist.github.com/rust-play/6f4932a58832875b81e37306cf6f16cd">https://gist.github.com/rust-play/6f4932a58832875b81e37306cf6f16cd</a></p>
<p>It would take something like an MCP to actually drive this into std, resolve many questions that are definitely outstanding and even raised by this new API, and eventually toward stabilization, but maybe there's a group that could be interested in this, and we could try something like an MCP process for libs for this? Posting here to solicit for interest.</p>



<a name="209500225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209500225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209500225">(Sep 09 2020 at 11:48)</a>:</h4>
<p>Supporting slice patterns would be so nice!</p>



<a name="209500399"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209500399" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209500399">(Sep 09 2020 at 11:50)</a>:</h4>
<p>I wonder how it would look to thread the <code>haystack</code> through the <code>SearchStep</code> so we could safely pull a <code>&amp;str</code> from a <code>SearchStep&lt;&amp;str&gt;</code> since it already has to guarantee it <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="209500683"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209500683" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209500683">(Sep 09 2020 at 11:53)</a>:</h4>
<p><code>SearchSteps</code> (the iterator) should have a <code>haystack</code> method, but I'm not sure when you'd have a <code>SearchStep</code> without access to the haystack</p>



<a name="209500801"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209500801" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209500801">(Sep 09 2020 at 11:55)</a>:</h4>
<p>I guess we could encapsulate the unsafe code is the point</p>



<a name="209500994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209500994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209500994">(Sep 09 2020 at 11:57)</a>:</h4>
<p>Yeh, I was just thinking as a way to avoid having to write <code>unsafe</code> yourself to pull the matching fragment out</p>



<a name="209501066"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501066" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501066">(Sep 09 2020 at 11:58)</a>:</h4>
<p>It's not sound unless we make all the fields private and give accessors</p>



<a name="209501080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501080">(Sep 09 2020 at 11:58)</a>:</h4>
<p>because someone can just mutate or create a SearchStep with the def in the gist</p>



<a name="209501145"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501145" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501145">(Sep 09 2020 at 11:59)</a>:</h4>
<p>However, <code>SearchSteps&lt;'a, str&gt;</code> could have methods which return strs instead of SearchSteps</p>



<a name="209501366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501366">(Sep 09 2020 at 12:01)</a>:</h4>
<p>have you compared with <a href="https://github.com/rust-lang/rfcs/issues/2500">rfc#2500</a> <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="209501444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501444">(Sep 09 2020 at 12:02)</a>:</h4>
<p>Ah right, I see. We don’t have any problem with inherent methods on iterators do we?</p>



<a name="209501464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501464">(Sep 09 2020 at 12:02)</a>:</h4>
<p>I knew there was prior work but I haven't read your RFC in quite a while kennytm</p>



<a name="209501465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501465">(Sep 09 2020 at 12:02)</a>:</h4>
<p>No, because <code>Chars</code> has a <code>as_str</code> method I think...</p>



<a name="209501623"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501623" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501623">(Sep 09 2020 at 12:04)</a>:</h4>
<p>I don’t think we’ve done any MCPs for Libs yet 🙂</p>



<a name="209501744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501744">(Sep 09 2020 at 12:05)</a>:</h4>
<p>We haven't. I think it would be interesting to try</p>



<a name="209501848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501848">(Sep 09 2020 at 12:06)</a>:</h4>
<p>kennytm it would be interesting to chat about why your API is so much more developed than this one, why you think all of that is needed</p>



<a name="209501920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501920">(Sep 09 2020 at 12:07)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="222471">@BurntSushi</span> since regex supports the <code>Pattern</code> API.</p>



<a name="209501970"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209501970" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209501970">(Sep 09 2020 at 12:08)</a>:</h4>
<p>For example, I tried actively to avoid creating Haystack traits</p>



<a name="209502186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> kennytm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502186">(Sep 09 2020 at 12:10)</a>:</h4>
<p>mainly to support splitting a <code>&amp;mut str</code> into 3 parts so that patterns can be applied on <code>&amp;mut str</code> (also <code>&amp;mut [T]</code>)</p>



<a name="209502310"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502310" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502310">(Sep 09 2020 at 12:12)</a>:</h4>
<p>This API is designed to work only on <code>&amp;(impl Hay)</code> types but not require the searcher to hold onto the reference</p>



<a name="209502330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502330">(Sep 09 2020 at 12:12)</a>:</h4>
<p>So you can just pass your <code>&amp;mut (impl Hay)</code> to <code>Search::step</code></p>



<a name="209502685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502685">(Sep 09 2020 at 12:16)</a>:</h4>
<p>I dropped the ball on participating in the RFC thread for 2500, but I'm inclined to agree with SimonSapin's "yagni" concern.</p>



<a name="209502728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502728">(Sep 09 2020 at 12:17)</a>:</h4>
<p>I'd like to see if we can support roughly the same requirements with a simpler API. I'm comfortable if that means some amount of code duplication internal to std.</p>



<a name="209502794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209502794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209502794">(Sep 09 2020 at 12:18)</a>:</h4>
<p>(also possibly we could privately use similar infrastructure to reduce duplication inside std while keeping the public API much simpler)</p>



<a name="209520628"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209520628" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209520628">(Sep 09 2020 at 14:44)</a>:</h4>
<p>If there is <code>start</code> and <code>end</code> if we implemented this for something like <code>find</code>, wouldn't one of the <code>start</code> or <code>end</code> become useless since we only have one item? Do we already need to range <code>start</code> and <code>end</code> for different conditions? It looks like <code>Range</code> + <code>bool</code> to me.</p>



<a name="209613292"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209613292" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209613292">(Sep 10 2020 at 09:17)</a>:</h4>
<p>pickfire: a Range + bool is equivalent to a start, an end, and a bool, since a range is a start and end pair</p>



<a name="209671798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209671798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209671798">(Sep 10 2020 at 16:22)</a>:</h4>
<p>Yes, but should we use <code>Range&lt;usize&gt;</code> here or just start and end? Range may be clearer since it have exactly start and end but may have compile time costs due to generics I guess?</p>



<a name="209686040"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209686040" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209686040">(Sep 10 2020 at 18:07)</a>:</h4>
<p>a monomorphized generic doesn't have extra cost, it's already a fully filled in type</p>



<a name="209735686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/209735686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Mannix <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#209735686">(Sep 11 2020 at 02:35)</a>:</h4>
<p>I think <code>Range&lt;usize&gt;</code> might be nice because it properly encodes the fact that start is inclusive and end is exclusive, and that the range is empty if the start is not less than the end and can be passed directly to <code>get</code> methods</p>



<a name="210595102"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/210595102" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Charles Lew <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#210595102">(Sep 19 2020 at 03:32)</a>:</h4>
<p>Hi, i want to try to push <code>OsStr</code> pattern matching forward. In the past RFC 2500 PR was implemented but closed because that PR was too large. So i'm trying to split that PR into smaller PRs. And first of them is now up at <a href="https://github.com/rust-lang/rust/issues/76901">#76901</a> . Since rfc2500 is an accepted rfc, i wonder if mcp or something is still needed here? thanks!</p>



<a name="210603267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/New%20Pattern%20API%3F/near/210603267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> boats <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/New.20Pattern.20API.3F.html#210603267">(Sep 19 2020 at 08:02)</a>:</h4>
<p>I think an MCP is beneficial because, tbh, I'm pretty skeptical of stabilizing RFC 2500 as is, because of the number of new public APIs it adds to std.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>